//
//  ContentView.swift
//  Memorize
//
//  Created by 姬向阳 on 2020/5/25.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @ObservedObject var emojiMemoryGame: EmojiMemoryGame
    
    var body: some View {
        VStack {
            Grid(emojiMemoryGame.cards) { card in
                CardView(card: card)
                    .onTapGesture {
                        withAnimation(.linear) {
                            self.emojiMemoryGame.chooseCard(card:card)
                        }
                }
                .padding(5)
            }
            .padding()
            Button(action: {
                withAnimation(.easeInOut) {
                    self.emojiMemoryGame.resetGame()
                }
            }) {
                Text("New Game")
                    .padding(5)
                    .overlay(
                        RoundedRectangle(cornerRadius: 3)
                            .stroke(lineWidth: 2)
                )
            }
        }
        .foregroundColor(.orange)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        let game = EmojiMemoryGame()
        game.chooseCard(card: game.cards[0])
        return ContentView(emojiMemoryGame: game)
    }
}

struct CardView: View {
    var card: MemoryGame<String>.Card
    
    @State private var animatedBonusRemaining: Double = 0
    
    var body: some View {
        GeometryReader { geometry in
            if self.card.isFaceUp || !self.card.isMatched {
                ZStack {
                    Group {
                        if self.card.isConsumingBonusTime {
                            Pie(startAngle: Angle(degrees: 0), endAngle: Angle(degrees: -self.animatedBonusRemaining*360), clockwise: true)
                                .onAppear {
                                    self.startBonusTimeAnimation()
                            }
                        } else {
                            Pie(startAngle: Angle(degrees: 0), endAngle: Angle(degrees: -self.card.bonusRemaining*360), clockwise: true)
                        }
                    }
                    .opacity(0.3)
                    .padding(5)
                    .transition(.identity)
                    Text(self.card.content)
                        .font(.system(size: self.fontSize(for: geometry.size)))
                        .rotationEffect(Angle(degrees: self.card.isMatched ? 360 : 0))
                        .animation(self.card.isMatched ? Animation.linear(duration: 1).repeatForever(autoreverses: false) : .default)
                }
                .cardify(isFaceUp: self.card.isFaceUp)
                .transition(.scale)
            }
        }
    }
    
    private func startBonusTimeAnimation() {
        animatedBonusRemaining = card.bonusRemaining
        withAnimation(.linear(duration: card.bonusTimeRemaining)) {
            animatedBonusRemaining = 0
        }
    }
    
    private func fontSize(for size: CGSize) -> CGFloat {
        return min(size.width, size.height) * 0.7
    }
}
